package com.ewhale.feitengguest.ui.user.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.LoginInfoDto;
import com.ewhale.feitengguest.dto.ThirdInfoDto;
import com.ewhale.feitengguest.presenter.user.mine.WithdrawPrensenter;
import com.ewhale.feitengguest.utils.Contants;
import com.ewhale.feitengguest.view.user.mine.WithdrawView;
import com.ewhale.feitengguest.widget.InputNameDialog;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.HawkKey;
import com.simga.library.utils.LogUtil;
import com.simga.library.utils.StringUtil;
import com.simga.library.widget.BGButton;

import java.util.Map;

import butterknife.BindView;
import cn.jiguang.share.android.api.AuthListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.model.AccessTokenInfo;
import cn.jiguang.share.android.model.BaseResponseInfo;

public class WithdrawActivity extends MBaseActivity<WithdrawPrensenter> implements WithdrawView {
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.radioGroup)
    RadioGroup mRadioGroup;
    @BindView(R.id.btn_commit)
    BGButton mBtnCommit;
    @BindView(R.id.rb_alipay)
    RadioButton rbAlipay;
    @BindView(R.id.btn_alipay)
    BGButton btnAlipay;
    @BindView(R.id.cl_alipay)
    ConstraintLayout clAlipay;
    @BindView(R.id.rb_wechat)
    RadioButton rbWechat;
    @BindView(R.id.btn_wechat)
    BGButton btnWechat;
    @BindView(R.id.cl_wechat)
    ConstraintLayout clWechat;

    private int money;
    private static final int REQUEST_CODE_BINGD_ALIACCOUNT = 1001;
    private int withType;
    private LoginInfoDto mInfoDto;
    private ThirdInfoDto thirdInfoDto;

    private InputNameDialog mInputNameDialog;


    public static void open(MBaseActivity context, int money) {
        Bundle bundle = new Bundle();
        bundle.putInt("money", money);
        context.startActivity(bundle, WithdrawActivity.class);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        money = bundle.getInt("money");
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("提现");
        mInfoDto = Hawk.get(HawkKey.USER_LOGIN_INFO, null);
        mTvPrice.setText(String.valueOf(money));
        presenter.getThirdInfo();
//        if(money == 1){
//            showToast("1元自动提现仅支付宝支持");
//        }
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    @Override
    protected void initListener() {
        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_alipay) {
                withType = 2;
            } else if (checkedId == R.id.rb_wechat) {
                withType = 1;
            }
        });
        mBtnCommit.setOnClickListener(v -> {
            if (withType == 0) {
                showToast("请选择提现方式");
                return;
            }
            presenter.whitDraw(withType, money);
        });
        btnAlipay.setOnClickListener(v -> BindAliAccountActivity.open(mContext, mInfoDto.getName(), REQUEST_CODE_BINGD_ALIACCOUNT));
        btnWechat.setOnClickListener(v -> {
            if (CheckUtil.isNull(thirdInfoDto.getWeChatOpenId())) {
                JShareInterface.authorize(Contants.weChat, mAuthListener);
            } else {
                if (mInputNameDialog == null) {
                    mInputNameDialog = new InputNameDialog(mContext, "请输入微信认证的真实姓名");
                }
                mInputNameDialog.setCommitCallBack(name -> presenter.bindWeChatName(name));
                mInputNameDialog.show();
            }
        });
    }

    AuthListener mAuthListener = new AuthListener() {
        @Override
        public void onComplete(Platform platform, int action, BaseResponseInfo data) {
            LogUtil.e("LOGIN", "onComplete:" + platform + ",action:" + action + ",data:" + data);
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    if (data instanceof AccessTokenInfo) {        //授权信息
                        presenter.bindThird(((AccessTokenInfo) data).getOpenid(), 1);
                    }
                    break;
            }
        }

        @Override
        public void onError(Platform platform, int action, int errorCode, Throwable error) {
            LogUtil.e("LOGIN", "onError:" + platform + ",action:" + action + ",error:" + error);
            String toastMsg = null;
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    toastMsg = "授权失败";
                    break;
                case Platform.ACTION_REMOVE_AUTHORIZING:
                    toastMsg = "删除授权失败";
                    break;
            }
            showToast(toastMsg);
        }

        @Override
        public void onCancel(Platform platform, int action) {
            String toastMsg = null;
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    toastMsg = "取消授权";
                    break;
            }
            showToast(toastMsg);
        }
    };

    @Override
    public void bindSuccess(Map<String, Object> params) {
        mInfoDto.setWechatId((String) params.get("openid"));
        Hawk.put(HawkKey.USER_LOGIN_INFO, mInfoDto);
        if (mInputNameDialog == null) {
            mInputNameDialog = new InputNameDialog(mContext, "请输入微信认证的真实姓名");
        }
        mInputNameDialog.setCommitCallBack(name -> presenter.bindWeChatName(name));
        mInputNameDialog.show();
    }

    @Override
    public void bindWeChatName() {
        showToast("绑定微信成功");
        rbWechat.setVisibility(View.VISIBLE);
        clWechat.setVisibility(View.GONE);
        mBtnCommit.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
        mBtnCommit.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
        mBtnCommit.setTextColor(Color.parseColor("#ffffff"));
        mBtnCommit.setEnabled(true);
    }

    @Override
    public void showThirdInfo(ThirdInfoDto thirdInfoDto) {
        this.thirdInfoDto = thirdInfoDto;
        if (CheckUtil.isNull(thirdInfoDto.getAliAccount())) {
            rbAlipay.setVisibility(View.GONE);
            clAlipay.setVisibility(View.VISIBLE);
        } else {
            rbAlipay.setText("支付宝    " + StringUtil.phone2Unknown(thirdInfoDto.getAliAccount()));
            rbAlipay.setVisibility(View.VISIBLE);
            clAlipay.setVisibility(View.GONE);
        }
        if (CheckUtil.isNull(thirdInfoDto.getWeChatTrueName())) {
            rbWechat.setVisibility(View.GONE);
            clWechat.setVisibility(View.VISIBLE);
        } else {
            rbWechat.setVisibility(View.VISIBLE);
            clWechat.setVisibility(View.GONE);
        }
        if (CheckUtil.isNull(thirdInfoDto.getAliAccount()) && CheckUtil.isNull(thirdInfoDto.getWeChatTrueName())) {
            mBtnCommit.setNormalSolid(Color.parseColor("#EEEEEE"));
            mBtnCommit.setNormalStroke(1, Color.parseColor("#D4D4D4"));
            mBtnCommit.setTextColor(Color.parseColor("#BBBBC2"));
            mBtnCommit.setEnabled(false);
        } else {
            mBtnCommit.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setTextColor(Color.parseColor("#ffffff"));
            mBtnCommit.setEnabled(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_BINGD_ALIACCOUNT) {
            showToast("绑定支付宝成功");
            rbAlipay.setVisibility(View.VISIBLE);
            clAlipay.setVisibility(View.GONE);
            mBtnCommit.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setTextColor(Color.parseColor("#ffffff"));
            mBtnCommit.setEnabled(true);
        }
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @Override
    public void withSuccess() {
        if (money == 1) {
            showToast("提现成功");
        } else {
            showToast("提现成功，请等待工作人员审核");
        }
        finish();
    }
}
